<?php

//易赛
class Yisai
{
    //支付宝
    private static $AppId = '5000160';
//    private static $AppId = '8888888';
    private static $AppKey = 'dcdc2c0e8d84bf2040b33d2b27a5897f';
//    private static $AppKey = 'test';
    private static $Url = 'http://llbchongzhi.esaipai.com/';

    /**
     * 话费/流量包缴费充值接口
     * @param $phone
     * @param $pid
     * @return string
     */
    public static function IRecharge_Flow($phone,$pid)
    {
        $url                        = self::$Url . 'Flow_Test/IRecharge_Flow';
        $curlData['UserNumber']     = self::$AppId;
        $curlData['OutOrderNumber'] = self::get_order_no('HF');
        $curlData['ProId']          = $pid;
        $curlData['PhoneNumber']    = $phone;
        $curlData['PayAmount']      = 1;
        $curlData['StartTime']      = date('Y-m-d H:i:s');
        $curlData['TimeOut']        = 300;
        $curlData['RecordKey']      = self::get_sign($curlData);

        return self::postRequest($url, $curlData);
    }



    /**
     * 获取签名
     * @param $params
     * @return false|string
     */
    private static function get_sign($params)
    {
        $str = '';
        foreach ($params as $val) {
            $str .= $val;
        }
        $str .= self::$AppKey;
        return substr(md5($str), 0, 16);
    }

    private static function xmltoarr($path){//xml字符串转数组
        $xml= $path;//XML文件
        $objectxml = simplexml_load_string($xml);//将文件转换成 对象
        $xmljson= json_encode($objectxml );//将对象转换个JSON
        $xmlarray=json_decode($xmljson,true);//将json转换成数组
        return $xmlarray;
    }

    /**
     * 用CURL模拟提交数据
     *
     * @param string $url post所要提交的网址
     * @param array $data 所要提交的数据
     * @param string $proxy 代理设置
     * @param integer $expire 所用的时间限制
     * @return string
     */
    private static function postRequest($url, $data = null, $type = 'array', $proxy = null, $expire = 30)
    {
        //参数分析
        if (!$url) {
            return false;
        }
        //cookie file
        $cookieFile = '/cache' . md5(time()) . '.txt';
        //分析是否开启SSL加密
        $ssl = substr($url, 0, 8) == 'https://' ? true : false;
        //读取网址内容
        $ch = curl_init();
        //设置代理
        if (!is_null($proxy)) {
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        if ($ssl) {
            // 对认证证书来源的检查
            //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            // 从证书中检查SSL加密算法是否存在
            //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
        }
        //cookie设置
        curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        //设置浏览器
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        //发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_POST, true);
        //Post提交的数据包
        if ($type == 'array') {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        //使用自动跳转
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $expire);
        $content = curl_exec($ch);
        curl_close($ch);

        return self::xmltoarr($content);
    }

    /**
     * 生成订单号
     * @param $title
     * @return string
     */
    private static function get_order_no($title)
    {
        $str = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        return $title . $str;
    }


}
